/*
* @Author: steven.wang
* @Date:   2019-05-08 16:04:40
* @Last Modified by:   steven.wang
* @Last Modified time: 2019-05-15 16:06:47
*/
const moment = require('moment');
const NaviModel = require('../../models/serve/navi');
const limit = require('../../config').page.limit;
const jsonSendFormat = require('../../utils/tools').jsonSendFormat;
/**
 * 获取网址分类列表
 * @author steven.wang
 * @DateTime 2019-05-15T15:48:19+0800
 */
exports.listCategory = async (req, res) => {
	try {
		let list = await NaviModel.listCategory({
			sorts: 'id desc'
		});
		list.map(item => {
			item.createAt = moment(item.createAt).format('YYYY-MM-DD HH:mm:ss');
			return item;
		});
		res.json(jsonSendFormat(200, list))
	} catch(err) {
		res.json(jsonSendFormat(500, err.sqlMessage || err))
	}
}
/**
 * 添加网址分类
 * @author steven.wang
 * @DateTime 2019-05-15T15:48:24+0800
 */
exports.addCategory = async (req, res) => {
	try {
		let result = await NaviModel.addCategory({
			conditions: req.body
		});
		console.log(result)
		res.json(jsonSendFormat(200))
	} catch(err) {
		console.log(err)
		res.json(jsonSendFormat(500, err.sqlMessage || err))
	}
}
/**
 * 获取网址列表
 * @author steven.wang
 * @DateTime 2019-03-23T03:45:07+0800
 */
exports.list = async (req, res) => {
	try {
		let conditions = [], likes = {};
		req.query.type && (conditions.type = [req.query.type]);
		req.query.name && (likes.name = req.query.name);
		let list = await NaviModel.list({
			conditions: conditions,
			likes: likes,
			sorts: req.query.sorts || 'createAt desc',
			pages: {
				page: req.query.page || 1,
				pageSize: req.query.pageSize || 10
			}
		});
		list.map(item => {
			item.createAt = moment(item.createAt).format('YYYY-MM-DD HH:mm:ss');
			return item;
		});
		let count = await NaviModel.count({
			conditions: conditions,
			likes: likes,
			sorts: req.query.sorts || 'createAt desc'
		});
		res.json(jsonSendFormat(200, {
			page: parseInt(req.query.page) || 1,
			pageSize: parseInt(req.query.pageSize) || limit,
			total: count[0]['count(*)'],
			list: list
		}))
	} catch(err) {
		res.json(jsonSendFormat(500, err.sqlMessage || err))
	}
}
/**
 * 添加网址导航
 * @author steven.wang
 * @DateTime 2019-03-23T03:45:07+0800
 */
exports.add = async (req, res) => {
	try {
		let result = await NaviModel.add({
			conditions: req.body
		});
		console.log(result)
		res.json(jsonSendFormat(200))
	} catch(err) {
		console.log(err)
		res.json(jsonSendFormat(500, err.sqlMessage || err))
	}
}
/**
 * 更新网址导航
 * @author steven.wang
 * @DateTime 2019-03-23T03:45:07+0800
 */
exports.update = async (req, res) => {
	try {
		let result = await NaviModel.update(req.body);
		res.json(jsonSendFormat(200))
	} catch(err) {
		res.json(jsonSendFormat(500, err.sqlMessage || err))
	}
}
/**
 * 删除网址导航
 * @author steven.wang
 * @DateTime 2019-03-23T03:45:07+0800
 */
exports.delete = async (req, res) => {
	try {
		let result = await NaviModel.delete(req.body);
		res.json(jsonSendFormat(200))
	} catch(err) {
		res.json(jsonSendFormat(500, err.sqlMessage || err))
	}
}